package severlet;

import bean.Express;
import dao.ExpressDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AdminExpressButtonServlet", value = "/AdminExpressButtonServlet")
public class AdminExpressButtonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //判断按钮里的value值
        String button = request.getParameter("submit");
        //获取前端数据
        String studentname = request.getParameter("studentname");
        String dormitoryid = request.getParameter("dormitoryid");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date arrivetime=null;
        Date collecttime=null;

        try {
             arrivetime = sdf.parse(request.getParameter("arrivetime"));
             collecttime=sdf.parse(request.getParameter("collecttime"));
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("要根据全部信息填完");
            System.out.println("=============输入时间有误==============");
            request.getRequestDispatcher("FanHui.jsp").forward(request,response);

        }
        int  collectnum =Integer.parseInt(request.getParameter("collectnum")) ;
        String collectname = request.getParameter("collectname");
        String collectphoneid = request.getParameter("collectphoneid");

        //封装
        Express express=new Express();
        express.setStudentname(studentname);
        express.setDormitoryid(dormitoryid);
        express.setArrivetime(arrivetime);
        express.setCollecttime(collecttime);
        express.setCollectnum(collectnum);
        express.setCollectname(collectname);
        express.setCollectphoneid(collectphoneid);

        //调用Dao层
        ExpressDao expressDao=new ExpressDao();
        if (button.equals("submit")){
            //提交
            expressDao.submit(express);
        }else if (button.equals("modify")){
            expressDao.modify(express);
            System.out.println("修改");
        }


        request.getRequestDispatcher("AdminExpressServlet").forward(request,response);
    }
}
